package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	businessOpportunityPb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/contact"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type ContactController struct {
	controller.BaseController
	contactService contact.ContactServiceIface
	reportService  report.ReportServiceIface
}

// NewContactController 构造函数
func NewContactController(
	contactService contact.ContactServiceIface,
	reportService report.ReportServiceIface,
) *ContactController {
	return &ContactController{
		contactService: contactService,
		reportService:  reportService,
	}
}

// RegisterRouter
// 注册路由
func (c *ContactController) RegisterRouter(g *gin.RouterGroup) {
	// 获取联系人列表
	g.POST("/get-list", c.GetContactList)
	// 联系人详情
	g.POST("/detail", c.ContactDetail)
	// 获取手机号
	g.POST("/get-mobile", c.GetMobile)
	// 合作案例
	g.POST("/cooperation-list", c.CooperationList)
}

func (c *ContactController) setAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *ContactController) GetContactList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	request := &dto.ContactListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	if len(request.Company) < 1 {
		stark.Logger.Errorf(ctx, "contact/get-list err:%s", "company参数异常")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "company参数异常")
		return
	}

	rs, err := c.contactService.GetContactList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/get-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) ContactDetail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	request := &businessOpportunityPb.ContactDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.ContactDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) CooperationList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	request := &businessOpportunityPb.CooperationListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.CooperationList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/cooperation-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) GetMobile(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	request := &businessOpportunityPb.GetMobileRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.GetMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/get-mobile err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}
